<template>
  <a-layout class="container">
    <!-- 头部   -->
    <a-layout-header class="header">
      <img class="logo" src="@/assets/imgs/logo.jpg" />
      {{ title }}
      <div class="right">
        <img class="icon" :src="user.avatar" />
        {{ user.name }}
        <a @click="logout">退出登录</a>
      </div>
    </a-layout-header>
    <a-layout>
      <!--   菜单   -->
      <a-layout-sider theme="light" class="sider" collapsible>
        <BaseMenu :data="menuTree" :collapsed="true" />
      </a-layout-sider>
      <!--  主面板    -->
      <a-layout class="main" style="padding: 10px 24px 0px">
        <!--  容器      -->
        <a-layout-content class="content">
          <router-view />
        </a-layout-content>
      </a-layout>
    </a-layout>
  </a-layout>
</template>
<script lang="ts" setup>
import '@/assets/scss/mixin.scss'
import { userStore } from '../../store'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { menu } from '@/entity/system/menu'
const title = import.meta.env.VITE_APP_TITLE
const router = useRouter()
// 获取用户信息
const store = userStore()
const user = store.getUser
// 获取菜单信息
const menuTree = filterMenu(JSON.parse(JSON.stringify(store.getMenu)))
/**
 * 过滤菜单（去除按钮）
 * @param data 菜单数据
 */
function filterMenu(data: Array<menu>): Array<menu> {
  // 过滤非管理端路由
  data = data.filter((val) => val.endpoint == 0)
  // 先找到所有非按钮菜单
  for (let parent of data) {
    // 将子菜单的数据插入到当前数据中
    if (parent.children && parent.children.length > 0 && parent.children[0].type == 2) {
      parent.children = []
    } else {
      filterMenu(parent.children as unknown as Array<menu>)
    }
  }
  return data
}
// 退出登录
function logout() {
  if (confirm('您确认退出登录吗?')) {
    store.removeAllInfo()
    router.push('/login')
    message.success('退出登录成功')
  }
}
</script>
<style>
.container {
  width: 100%;
  height: 100%;
  overflow: auto;
  font-family: '微软雅黑';
}
.header {
  font-size: 20px;
  background-color: white;
  box-shadow: 0px -70px 100px black;
  z-index: 100;
}
.right {
  float: right;
}
a {
  margin-left: 10px;
  color: black !important;
  font-size: 15px;
  font-weight: normal;
}
.logo,
.icon {
  width: 40px;
  height: 40px;
  border-radius: 200%;
  margin: 0px 10px;
  margin-left: -30px;
}
.sider {
  max-height: 100vh;
  overflow-y: auto;
  overflow-x: hidden;
  width: 200px;
}
.main {
  height: auto !important;
  margin-bottom: 10px;
}
.content {
  height: auto !important;
  background: white;
  padding: 15px;
  overflow-y: hidden;
}
</style>
